package start;

import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.support.CustomSQLErrorCodesTranslation;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;

public class Test02 {
    private class MyCustomSQLErrorCodesTranslator extends SQLErrorCodeSQLExceptionTranslator {
        protected DataAccessException customTranslate(String task, String sql, SQLException sqlex) {
            if (sqlex.getErrorCode() == -12345) {
                return new DeadlockLoserDataAccessException(task, sqlex);
            }
            return null;
        }
    }
    public Test02() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://192.168.1.101:3306/myTest");
        dataSource.setUsername("root");
        dataSource.setPassword("2348");
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        MyCustomSQLErrorCodesTranslator tr = new MyCustomSQLErrorCodesTranslator();
        tr.setDataSource(dataSource);
        jdbcTemplate.setExceptionTranslator(tr);        
    }
}
